package com.fand.manager.controller;


import java.util.List;

import javax.annotation.Resource;

import com.fand.common.util.PageResult;
import com.fand.common.util.ResultHelp;
import com.fand.manager.model.menu.SysMenuReturnDTO;
import com.fand.manager.model.role.SysRoleDeleteDTO;
import com.fand.manager.model.role.SysRolePostDTO;
import com.fand.manager.model.role.SysRoleReturnDTO;
import com.fand.manager.model.role.SysRoleSearchDTO;
import com.fand.manager.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author javaboy
 * @since 2021-06-07
 */
@Api(tags = "角色")
@RestController
@RequestMapping("/sys-role")
public class SysRoleController {

    @Resource
    private ISysRoleService sysRoleService;

    @ApiOperation("保存角色")
    @PreAuthorize("hasAuthority('sys:role:add')")
    @PostMapping("/save")
    public ResultHelp<SysMenuReturnDTO> saveRole(@RequestBody SysRolePostDTO sysRolePostDTO){
        return ResultHelp.success(sysRoleService.saveRole(sysRolePostDTO));
    }

    @ApiOperation("角色分页查询")
    @PreAuthorize("hasAuthority('sys:role:query')")
    @GetMapping("/list")
    public PageResult<List<SysRoleReturnDTO>> listRoles(SysRoleSearchDTO sysRoleSearchDTO){
        return sysRoleService.listRoles(sysRoleSearchDTO.builder());
    }
    @ApiOperation("删除角色")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    @PostMapping("/deleteByIds")
    public ResultHelp<Boolean> deleteByIds(@RequestBody SysRoleDeleteDTO sysRoleDeleteDTO){
        return ResultHelp.success(sysRoleService.deleteByIds(sysRoleDeleteDTO.getRoleIds()));
    }

    @ApiOperation("查询角色")
    @PreAuthorize("hasAuthority('sys:role:query')")
    @GetMapping("/findRole")
    public ResultHelp<SysRoleReturnDTO> findRole(@RequestParam Long id){
        return ResultHelp.success(sysRoleService.findRole(id));
    }

    @ApiOperation("修改角色")
    @PreAuthorize("hasAuthority('sys:role:update')")
    @PostMapping("/update")
    public ResultHelp<Boolean> updateRole(@RequestBody SysRolePostDTO sysRolePostDTO){
        return ResultHelp.success(sysRoleService.updateRole(sysRolePostDTO));
    }

}

